package org.jeecg.modules.wangyue.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.*;
import org.jeecg.modules.wangyue.entity.QualificationReviewJs;
import org.jeecg.modules.wangyue.entity.ServiceJs;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.service.IQualificationReviewJsService;
import org.jeecg.modules.wangyue.service.IServiceJsService;
import org.jeecg.modules.wangyue.service.IUserRoleInfoService;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Description: 技师资质审核
* @Author: jeecg-boot
* @Date:   2023-06-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="技师资质审核")
@RestController
@RequestMapping("/wangyue/sys/jsReview")
public class JsReviewController extends JeecgController<QualificationReviewJs, IQualificationReviewJsService> {
   @Autowired
   private IQualificationReviewJsService qualificationReviewJsService;
   @Autowired
   private ExportUtil exportUtil;


    /**
     * 技师资质审核-分页列表查询
     * @param keywords		关键字
     * @param reviewStatus	审核状态(0-待审核,1-审核通过,2-驳回)
     * @param pageNo
     * @param pageSize
     * @return
     */
   @AutoLog(value = "技师资质审核-分页列表查询")
   @ApiOperation(value="技师资质审核-分页列表查询", notes="技师资质审核-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(@RequestParam(name="keywords", required = false) String keywords,
                                  @RequestParam(name="reviewStatus", required = false) String reviewStatus,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
       QueryWrapper<QualificationReviewJs> queryWrapper = new QueryWrapper<>();
       if(oConvertUtils.isNotEmpty(keywords)){
           String realNameSql = "real_name like '%" + keywords + "%'";
           String phoneSql = "phone like '%" + keywords + "%'";
           queryWrapper.apply("(" + realNameSql + " or " + phoneSql + ")");
       }
       if(oConvertUtils.isNotEmpty(reviewStatus)){
           queryWrapper.eq("review_status", reviewStatus);
       }
       queryWrapper.eq("del_flag", DelFlagConstant.normal);
       queryWrapper.orderByDesc("create_time");
       Page<QualificationReviewJs> page = new Page<QualificationReviewJs>(pageNo, pageSize);
       IPage<QualificationReviewJs> pageList = qualificationReviewJsService.page(page, queryWrapper);
       return Result.OK(pageList);
   }


    /**
     * 技师资质审核-通过/驳回
     *
     * @param qualificationReviewJs
     * @return
     */
    @AutoLog(value = "技师资质审核-通过/驳回")
    @ApiOperation(value="技师资质审核-通过/驳回", notes="技师资质审核-通过/驳回")
    @PostMapping(value = "/editReviewStatus")
    public Result<?> editReviewStatus(@RequestBody QualificationReviewJs qualificationReviewJs) {
        if(ReviewStatusConstant.reject.equals(qualificationReviewJs.getReviewStatus())){ //操作-驳回
            UpdateWrapper<QualificationReviewJs> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", qualificationReviewJs.getId());
            updateWrapper.eq("del_flag", DelFlagConstant.normal);
            updateWrapper.set("review_status", qualificationReviewJs.getReviewStatus());
            updateWrapper.set("update_time", DateUtil.getTime("yyyy-MM-dd HH:mm:ss"));
            if(qualificationReviewJsService.update(updateWrapper)){
                return Result.OK("驳回-操作成功");
            }
        }else if(ReviewStatusConstant.pass.equals(qualificationReviewJs.getReviewStatus())) { //操作-通过
            if(qualificationReviewJsService.passReviewStatus(qualificationReviewJs)){
                return Result.OK("通过-操作成功");
            }
        }
        return Result.error("操作失败");
    }



    /**
     * 导出excel
     *
     * @param qualificationReviewJs
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(QualificationReviewJs qualificationReviewJs) {
        QueryWrapper<QualificationReviewJs> queryWrapper = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(qualificationReviewJs.getIds())){
            queryWrapper.in("id", Arrays.asList(qualificationReviewJs.getIds().split(",")));
        }
        if(oConvertUtils.isNotEmpty(qualificationReviewJs.getKeywords())){
            String realNameSql = "real_name like '%" + qualificationReviewJs.getKeywords() + "%'";
            String phoneSql = "phone like '%" + qualificationReviewJs.getKeywords() + "%'";
            queryWrapper.apply("(" + realNameSql + " or " + phoneSql + ")");
        }
        if(oConvertUtils.isNotEmpty(qualificationReviewJs.getReviewStatus())){
            queryWrapper.eq("review_status", qualificationReviewJs.getReviewStatus());
        }
        queryWrapper.eq("del_flag", DelFlagConstant.normal);
        queryWrapper.orderByDesc("create_time");
        List<QualificationReviewJs> qualificationReviewJsList = qualificationReviewJsService.list(queryWrapper);

        //处理url数据，添加文件地址前缀
        if(oConvertUtils.isNotEmpty(qualificationReviewJsList) && qualificationReviewJsList.size() > 0){
            for(QualificationReviewJs qrj : qualificationReviewJsList){
                if (oConvertUtils.isNotEmpty(qrj.getRealHeadPicUrl())){
                    qrj.setRealHeadPicUrl(CommonConstant.fileAddressPrefix + qrj.getRealHeadPicUrl());
                }
                if (oConvertUtils.isNotEmpty(qrj.getIdCardFrontUrl())){
                    qrj.setIdCardFrontUrl(CommonConstant.fileAddressPrefix + qrj.getIdCardFrontUrl());
                }
                if (oConvertUtils.isNotEmpty(qrj.getIdCardBackUrl())){
                    qrj.setIdCardBackUrl(CommonConstant.fileAddressPrefix + qrj.getIdCardBackUrl());
                }
                if (oConvertUtils.isNotEmpty(qrj.getVideoAuthUrl())){
                    qrj.setVideoAuthUrl(CommonConstant.fileAddressPrefix + qrj.getVideoAuthUrl());
                }
                if (oConvertUtils.isNotEmpty(qrj.getWorkProveUrl())){
                    qrj.setWorkProveUrl(CommonConstant.fileAddressPrefix + qrj.getWorkProveUrl());
                }
                if (oConvertUtils.isNotEmpty(qrj.getHealthProveUrl())){
                    qrj.setHealthProveUrl(CommonConstant.fileAddressPrefix + qrj.getHealthProveUrl());
                }
            }
        }

        return exportUtil.exportXls(qualificationReviewJsList, QualificationReviewJs.class, "技师资质审核数据");
    }





}
